﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SIEGate.Core;
using SIEGate.Core.Notifications;

namespace SIEGateCoreTest.NotificationTests
{
    [TestClass]
    public class NotificationTests
    {
        private NotificationDispatcher m_dispatcher;
        [TestInitialize]
        public void TestInitialization()
        {
            m_dispatcher = new NotificationDispatcher();
        }

        [TestMethod]
        public void TestGetReceiverMethods()
        {
            IConfigInfo cfg = new MockAdapter.MockAdapterInfo(new HashSet<Guid>(){Guid.NewGuid()},new HashSet<Guid>(){Guid.NewGuid()} );
            NotificationDispatcher dispatcher = new NotificationDispatcher();
            INotificationReceiver receiver = dispatcher.GetReceiver(cfg);
        }

        [TestMethod]
        public void TestBasicBroadcast()
        {
            bool OK = false;
            IConfigInfo cfg = new MockAdapter.MockAdapterInfo(new HashSet<Guid>() { Guid.NewGuid() }, new HashSet<Guid>() { Guid.NewGuid() });
            INotificationReceiver receiver = m_dispatcher.GetReceiver(cfg);
            receiver.RegisterBroadcastHandler(BroadcastNotification.ConfigChange, (args) =>
            {
                OK = true;
            });
            m_dispatcher.SendBroadcast(BroadcastNotification.ConfigChange);
            Assert.AreEqual(true, OK);
        }

        [TestMethod]
        public void TestRoutedBroadcast()
        {
            bool sig1 = false;
            bool sig2 = false;

            IConfigInfo cfg1 = new MockAdapter.MockAdapterInfo(new HashSet<Guid>() { Guid.NewGuid() }, new HashSet<Guid>() { Guid.NewGuid() });
            IConfigInfo cfg2 = new MockAdapter.MockAdapterInfo(new HashSet<Guid>() { Guid.NewGuid() }, new HashSet<Guid>() { Guid.NewGuid() });
            cfg1.ID = 1;
            cfg2.ID = 2;
            INotificationReceiver r1 = m_dispatcher.GetReceiver(cfg1);
            INotificationReceiver r2 = m_dispatcher.GetReceiver(cfg2);

            r1.RegisterTargetedHandler("foo", (args) =>
            {
                sig1 = true;

            });
            r2.RegisterTargetedHandler("foo", (args) =>
            {
                sig2 = true;
            });

            r1.SendNotification(2, "foo");
            Assert.AreEqual(true, sig2);
            Assert.AreEqual(false, sig1);
        }

        [TestMethod]
        public void BroadCastErrorIsNotTermnal()
        {
            bool  sig1 = false, sig2 = false;
            INotificationReceiver r1, r2;
            IConfigInfo cfg1 = new MockAdapter.MockAdapterInfo(new HashSet<Guid>() { Guid.NewGuid() }, new HashSet<Guid>() { Guid.NewGuid() });
            IConfigInfo cfg2 = new MockAdapter.MockAdapterInfo(new HashSet<Guid>() { Guid.NewGuid() }, new HashSet<Guid>() { Guid.NewGuid() });


            r1 = m_dispatcher.GetReceiver(cfg1);
            r2 = m_dispatcher.GetReceiver(cfg2);

            r1.RegisterBroadcastHandler(BroadcastNotification.ConfigChange, args =>
            {
                throw new Exception("If you see this, bad things are afoot");
                sig1 = true;
            });
            r2.RegisterBroadcastHandler(BroadcastNotification.ConfigChange, args =>
            {
                sig2 = true;
            });

            m_dispatcher.SendBroadcast(BroadcastNotification.ConfigChange);
            Assert.AreEqual(true, sig2);

        }
    }
}
